package com.ciko.code.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import com.squareup.picasso.Transformation;

/**
 * Created by wayne on 2015/7/17.
 */
public class RoundPicassoTransformation implements Transformation {
    @Override
    public Bitmap transform(Bitmap source) {
//        Bitmap output = Bitmap.createBitmap(source.getWidth(),
//                source.getHeight(), Bitmap.Config.ARGB_8888);
//        Canvas canvas = new Canvas(output);
//        final Paint paint = new Paint();
//        final Rect rect = new Rect(0, 0, source.getWidth(),
//                source.getHeight());
//        final RectF rectF = new RectF(new Rect(0, 0, source.getWidth(),
//                source.getHeight()));
//        final float roundPx = 14;
//        paint.setAntiAlias(true);
//        canvas.drawARGB(0, 0, 0, 0);
//        paint.setColor(Color.BLACK);
//        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//
//        final Rect src = new Rect(0, 0, source.getWidth(),
//                source.getHeight());
//
//        canvas.drawBitmap(source, src, rect, paint);

        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        /**
         * 产生一个同样大小的画布
         */
        Canvas canvas = new Canvas(output);
        /**
         * 首先绘制圆形
         */
        canvas.drawCircle(source.getWidth() / 2, source.getWidth() / 2, source.getWidth() / 2, paint);
        /**
         * 使用SRC_IN
         */
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        /**
         * 绘制图片
         */
        canvas.drawBitmap(source, 0, 0, paint);
        if (output != source) {
            source.recycle();
        }

        return output;
    }

    @Override
    public String key() {
        return "square()";
    }
}
